package org.feit.findword;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.ListIterator;

import org.feit.findword.data.Score;
import org.feit.findword.game.Game;
import org.feit.findword.trie.Solution;

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class WordsListAdapter extends BaseAdapter {

	static class ViewHolder {
		TextView word;
		TextView points;
	}

	private LayoutInflater mInflater;
	private int mSize;
	private LinkedHashMap<String, Solution> mSolutions;
	private HashSet<String> mWords;
	private String[] mWordsList;
	
	public WordsListAdapter(Context context, LinkedHashMap<String, Solution> solutions, List<String> words) {
		mInflater = LayoutInflater.from(context);
		mSolutions = solutions;
		mWords = new HashSet<String>();
		for(String w : words) {
			mWords.add(w);
		}
		mWordsList = new String[mSolutions.keySet().size()];
		mSolutions.keySet().toArray(mWordsList);
		mSolutions = solutions;
		mSize = mWordsList.length;
	}
	
	@Override
	public int getCount() {
		return mSize; 
	}

	@Override
	public Object getItem(int pos) {
		return mWordsList[pos];
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	public String getWord(int pos) {
		return mWordsList[pos];
	}
	
	public Solution getSolution(int position) {
		return mSolutions.get(mWordsList[position]);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder;
		// When convertView is not null, we can reuse it directly, there is no
		// need
		// to reinflate it. We only inflate a new View when the convertView
		// supplied
		// by ListView is null.
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.word_item, null);

			// Creates a ViewHolder and store references to the two children
			// views
			// we want to bind data to.
			holder = new ViewHolder();
			holder.word = (TextView)convertView.findViewById(R.id.word);
			holder.points = (TextView) convertView.findViewById(R.id.points);
			convertView.setTag(holder);
		} else {
			// Get the ViewHolder back to get fast access to the TextView
			// and the ImageView.
			holder = (ViewHolder) convertView.getTag();
		}
		String word = mWordsList[position];
		if(mWords.contains(word)) {
			holder.word.setTextColor(0xffffff00);
		}else{
			holder.word.setTextColor(0xffffffff);
		}
		holder.word.setText(word);
		holder.points.setText(String.format("%d", Game.wordPoints(word.length())));
		return convertView;
	}
}
